﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tong;
using Tong.ThisSiteUse;
public partial class aspnet_testPargrameDir_Faq_Default : System.Web.UI.Page
{
    /// <summary>
    /// 数据操作通用类 实例 TextData
    /// </summary> 
    public TongUse TextData = new TongUse();

    /// <summary>
    /// 系统安装绝对路径
    /// </summary>
    public string Abs_Path = TongUse.SysPath;
    /// <summary>
    /// 用户存放数据的临时表
    /// </summary>
    DataTable MyDt = new DataTable();



    /// <summary>
    /// 指定得到目录
    /// </summary>
    public String GetUrl; //= "http://localhost:2284/NetDataBock/aspnet/admin/itemP/OnFAQ.aspx?id=";

    public SiteTong TextThisSite = new SiteTong();

    protected void Page_Load(object sender, EventArgs e)
    {

        PageSave();
    }

    /// <summary>
    /// 保存网页数据   这里要经常改
    /// </summary>
    void PageSave()
    {
        GetUrl = TextData.SqlataSet("select * from [CX_Column] where CX_Column_id=3").Tables[0].Rows[0]["CX_Column_ItemTemplate_Url"].ToString().Trim()+"?id="; //得到在线提问的 URL 
        string xxurl = TextThisSite.SiteGetColumnDirLink("3");//"/NetDataBock/html/answer/";
        string action = "";
        string id = "";
         string sql ="";
         try
         {
             //添加
             try
             {
                 action = Request.QueryString["action"].Trim();
             }
             catch
             { 
             
             }
             if (action != "Up")
             {
                 // Response.Write("insert into [tab] (" + SqlInsertGetPostAllKeys() + ") values(" + SqlInsertGetPostAllValues() + ")");
                 sql = "insert into [CX_FAQonLine] (" + SqlInsertGetPostAllKeys() + ") values(" + SqlInsertGetPostAllValues() + ")";
                 TextData.SqlDataSave(sql);

                // Response.Write(GetUrl);

                 id = TextData.SqlataSet("select top 1 Cx_FAQonLine_id from [CX_FAQonLine] order by Cx_FAQonLine_id desc ").Tables[0].Rows[0][0].ToString();//id
                 TextData.GetWebDataWrite((GetUrl + id), Server.MapPath(xxurl + id + ".html"));
                 // TextData.GetWebDataWrite((GetUrl + id), Server.MapPath("../../../../html/answer/" + id + ".html"));
                 Response.Write("window.location.href='" + xxurl + id + ".html'");//生成后就去
             }
             else
             {
                 id = Request.QueryString["id"].Trim();
                 int.Parse(id);
                 //更新
                 action = Request.QueryString["action"].Trim();
                 if (action == "Up")
                 {
                     string FormsValueAndkey = "";
                     for (int i = 0; i < Request.Form.Count; i++)
                     {
                         FormsValueAndkey += Request.Form.GetKey(i) + "='" + Request.Form.Get(i).Replace("'", "").Replace("\n", "<br/>") + "',";
                     }
                     //得到 Set 后的值
                     FormsValueAndkey = TextData.ForMatText(FormsValueAndkey, 0, (FormsValueAndkey.Length - 1)).Replace("...", "");
                     sql = "Update [CX_FAQonLine] Set " + FormsValueAndkey + "  where Cx_FAQonLine_id=" + id;
                     //Response.Write(sql);
                     TextData.SqlDataSave(sql);
                     TextData.GetWebDataWrite((GetUrl + id), Server.MapPath(xxurl + id + ".html"));
                     //TextData.GetWebDataWrite((GetUrl + id), Server.MapPath("../../../../html/answer/" + id + ".html"));
                     Response.Write("window.location.href='" + xxurl + id + ".html';");//生成后就去

                 }
             }
         }
         catch// (Exception ex)
         {
             try
             {
                 id = Request.QueryString["id"].Trim();
                 int.Parse(id);
                 //更新
                 action = Request.QueryString["action"].Trim();
                 if (action == "Up")
                 {
                     string FormsValueAndkey = "";
                     for (int i = 0; i < Request.Form.Count; i++)
                     {
                         FormsValueAndkey += Request.Form.GetKey(i) + "='" + Request.Form.Get(i).Replace("'", "").Replace("\n", "<br/>") + "',";
                     }
                     //得到 Set 后的值
                     FormsValueAndkey = TextData.ForMatText(FormsValueAndkey, 0, (FormsValueAndkey.Length - 1)).Replace("...", "");
                     sql = "Update [CX_FAQonLine] Set " + FormsValueAndkey + "  where Cx_FAQonLine_id=" + id;
                     //Response.Write(sql);
                     TextData.SqlDataSave(sql);
                     TextData.GetWebDataWrite((GetUrl + id), Server.MapPath(xxurl + id + ".html"));
                    // TextData.GetWebDataWrite((GetUrl + id), Server.MapPath("../../../../html/answer/" + id + ".html"));
                     Response.Write("window.location.href='" + xxurl + id + ".html'");//生成后就去

                 }
             }
             catch
             {
                 
             }
            // Response.Write(ex.Message);
         }
    }



    /// <summary>
    /// 得到POST 字段 inseer 语句
    /// </summary>
    /// <returns></returns>
    public string SqlInsertGetPostAllKeys()
    {
        string FormsKeyStr = "";
        for (int i = 0; i < Request.Form.Count; i++)
        {
            FormsKeyStr += Request.Form.GetKey(i) + ",";
            
        }
        if (FormsKeyStr.Length > 0)
        {
            return TextData.ForMatText(FormsKeyStr, 0, (FormsKeyStr.Length - 1)).Replace("...", "");//返加字符串
        }
        else
        {
            return "";
        }
    }
    /// <summary>
    /// 得到POST 字段值 , 只能全部是字符串 
    /// 用于 insert
    /// </summary>
    /// <returns></returns>
    public string SqlInsertGetPostAllValues()
    {
        string FormsKeyStr = "";
        for (int i = 0; i < Request.Form.Count; i++)
        {
            FormsKeyStr +="'"+ Request.Form.Get(i).Replace("'","").Replace("\n","<br/>") + "',";

        }
        if (FormsKeyStr.Length >0)
        {
            return TextData.ForMatText(FormsKeyStr, 0, (FormsKeyStr.Length - 1)).Replace("...","");//返加字符串
        }
        else
        {
            return "";
        }
    }
}
